package com.icesoft.system.service.impl;

import com.icesoft.framework.core.service.impl.BaseServiceImpl;
import com.icesoft.framework.core.util.TreeUtil;
import com.icesoft.system.entity.Company;
import com.icesoft.system.entity.Org;
import com.icesoft.system.entity.SubOrg;
import com.icesoft.system.handler.CompanyUpdateHandler;
import com.icesoft.system.mapper.OrgMapper;
import com.icesoft.system.service.OrgService;
import com.icesoft.system.vo.OrgTreeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * appuserService业务层处理
 *
 * @author madiao
 * @date 230212
 */
@Slf4j
@Service
public class OrgServiceImpl extends BaseServiceImpl<OrgMapper, Org> implements OrgService, CompanyUpdateHandler {

	@Resource
	private SubOrgServiceImpl subOrgService;

	public List<Long> findAllSubOrg(Long orgId) {
		return subOrgService.findAllSubOrgIds(orgId);
	}

	public List<Org> findAllSubOrgs(Long orgId) {
		List<Long> allSubOrg = subOrgService.findAllSubOrgIds(orgId);
		List<Org> list = new ArrayList<>();
		for (Long ids : allSubOrg) {
			list.add(getEntity(ids));
		}
		return list;
	}

	@Override
	public boolean createEntity(Org entity) {
		if (entity.getPid() != 0L) {
			Org parent = getEntity(entity.getPid());
			parent.setIsLeaf(false);
			updateEntity(parent);
		}
		boolean isOK = super.createEntity(entity);
		this.createSubOrg(entity);
		return isOK;
	}

	@Override
	public boolean updateEntity(Org sysOrg) {
		Org daoSysOrg = getEntity(sysOrg.getId());
		daoSysOrg.setName(sysOrg.getName());
		if (sysOrg.getPid() != 0L) {
			if (!Objects.equals(daoSysOrg.getPid(), sysOrg.getPid())) {
				daoSysOrg.setPid(sysOrg.getPid());
				subOrgService.deleteByOrgId(sysOrg.getId());
				this.createSubOrg(daoSysOrg);
			}
		}
		return super.updateEntity(sysOrg);
	}

	public void createSubOrg(Org daoSysOrg) {
		subOrgService.createEntity(new SubOrg(daoSysOrg.getId(), daoSysOrg.getId()));
		List<SubOrg> orgList = subOrgService.findAllParentOrg(daoSysOrg.getPid());
		for (SubOrg subOrg : orgList) {
			subOrgService.createEntity(new SubOrg(subOrg.getOrgId(), daoSysOrg.getId()));
		}
	}

	@Override
	public boolean deleteEntity(Serializable id) {
		subOrgService.deleteByOrgId(id);
		return super.deleteEntity(id);
	}

	public List<OrgTreeVO> getOrgTree() {
		List<Org> list = lambdaQuery().list();
		return TreeUtil.formatTree(list, OrgTreeVO.class);
	}

	@Override
	public void createCompany(Company entity) {
		Org org = new Org();
		if (entity.getId() == 1L) {
			org.setId(1L);
		}
		org.setCompanyId(entity.getId());
		org.setEditable(true);
		org.setPid(0L);
		org.setName(entity.getName());
		org.setSort(1);
		org.setStatus(true);
		this.createEntity(org);
	}

	@Override
	public void updateCompany(Company entity) {
		CompanyUpdateHandler.super.updateCompany(entity);
	}

	@Override
	public void deleteCompany(Serializable id) {
		CompanyUpdateHandler.super.deleteCompany(id);
		if (this.lambdaQuery().eq(Org::getCompanyId, id).count() > 0) {
			deleteEntities(this.lambdaQuery().eq(Org::getCompanyId, id));
		}
	}
}
